﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;

namespace Apps.BLL.PUB
{
    public partial class PUB_OF_DETAILBLL
    {

        public override List<PUB_OF_DETAILModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            IQueryable<PUB_OF_DETAIL> queryData = null;
            string pid = parentId.ToString();
            if (pid != "0")
            {
                queryData = m_Rep.GetList(a => a.TEMPLATE_ID == pid);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                            a => (
                                    a.DETAIL_ID.Contains(queryStr)
                                   || a.TEMPLATE_ID.Contains(queryStr)
                                   || a.TACHE_ID.Contains(queryStr)
                                   || a.ENABLED_NODES.Contains(queryStr)
                                   || a.CHECK_DETAIL_ORDER.Contains(queryStr)
                                   || a.REMARKS.Contains(queryStr)
                                 )
                            );
            }
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
        public override List<PUB_OF_DETAILModel> CreateModelList(ref IQueryable<PUB_OF_DETAIL> queryData)
        {
           
            List<PUB_OF_DETAILModel> modelList = (from r in queryData
                                                  select new PUB_OF_DETAILModel
                                                  {
                                                      REMARKS = r.REMARKS,
                                                      CHECK_DETAIL_ORDER = r.CHECK_DETAIL_ORDER,
                                                      ENABLED_NODES = r.ENABLED_NODES,
                                                      TACHE_ID = r.TACHE_ID,
                                                      DETAIL_ORDER = r.DETAIL_ORDER,
                                                      TEMPLATE_ID = r.TEMPLATE_ID,
                                                      DETAIL_ID = r.DETAIL_ID,
                                                      OF_TEMPLATEName = r.PUB_OF_TEMPLATE.TEMPLATE_NAME,
                                                      TACHE_NAME = r.PUB_OF_TACHE.TACHE_CODE,
                                                      
                                                  }).ToList();
            return modelList;
        }
        public override PUB_OF_DETAILModel GetById(object id)
        {
            if (IsExists(id))
            {

                PUB_OF_DETAIL entity = m_Rep.GetById(id);
                PUB_OF_DETAILModel model = new PUB_OF_DETAILModel();
                model.REMARKS = entity.REMARKS;
                model.CHECK_DETAIL_ORDER = entity.CHECK_DETAIL_ORDER;
                model.ENABLED_NODES = entity.ENABLED_NODES;
                model.TACHE_ID = entity.TACHE_ID;
                model.DETAIL_ORDER = entity.DETAIL_ORDER;
                model.TEMPLATE_ID = entity.TEMPLATE_ID;
                model.DETAIL_ID = entity.DETAIL_ID;
                model.OF_TEMPLATEName = entity.PUB_OF_TEMPLATE.TEMPLATE_NAME;
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}

